using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace yasaCS
{
    public partial class Form1 : Form
    {
        string filename = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_YASA_Click(object sender, EventArgs e)
        {
            string strAns;
            int ret = -1;
            int bUTF8 = rBtnUTF8.Checked ? 1 : 0;
            int iMaxLen = Convert.ToInt16(txtMaxLength.Text);

            YASAWrapper yasa = new YASAWrapper();
            if (File.Exists(filename))
                ret = yasa.DoSuffix(filename, bUTF8, iMaxLen, out strAns);
            else if (txtSource.TextLength > 0)
            {
                filename = Application.StartupPath + @"\yasa.txt";
                using (StreamWriter SW = File.CreateText(filename))
                {
                    SW.WriteLine(txtSource.Text);
                }
                txtSourceFile.Text = filename;
                ret = yasa.DoSuffix(filename, bUTF8, iMaxLen, out strAns);
            }
            else
            {
                MessageBox.Show("Invalid filepath and/or TextBox is empty!", "ERROR", MessageBoxButtons.OK);
                return;
            }
            if (ret != 1)
            {
                MessageBox.Show("Something bad happened inside yasa: error code: " + ret, "Panic", MessageBoxButtons.OK);
                return;
            }
            using (StreamWriter SW = File.CreateText(Application.StartupPath + @"\yasa.sfx"))
            {
                SW.Write(strAns);
            }
            //txtResult.Text = File.ReadAllText(Application.StartupPath + @"\yasa.sfx");
            txtResult.Text = strAns.Replace("\n", Environment.NewLine);
            GenerateListView(strAns);
            filename = "";
        }

        private void GenerateListView(string str)
        {
            lvResult.Items.Clear();
            string[] strArr= str.Split("\n".ToCharArray());
            foreach (string temp in strArr)
            {
                if (temp.IndexOf('\t') > -1)
                {
                    string[] strField = temp.Split("\t".ToCharArray());
                    lvResult.Items.Add(strField[0]).SubItems.Add(strField[1]);
                }
            }

        }
        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                filename = openFileDialog1.FileName;
                txtSourceFile.Text = filename;
            }
        }
        private string ConvertFromTo(string source, Encoding encFrom, Encoding encTo)
        {
            byte[] srcArray = encFrom.GetBytes(source);
            byte[] tarArray = Encoding.Convert(encFrom, encTo, srcArray);

            return encTo.GetString(tarArray);
        }
    }
}